<template>
  <div class="app-container-lr">
    <!-- 查询 -->
    <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" v-if="isHeaderDisplay" :isDisplayDefaultSearch="false">
      <template slot="search">
        <el-form-item label="关键词" label-width="70px">
          <el-input v-model="queryParam.qry_key" placeholder="请输入问题的关键词" style="width:220px" clearable></el-input>
        </el-form-item>
        <el-form-item label="开始时间" label-width="70px" v-if="refName === 'feedbackHistory'">
          <el-date-picker v-model="queryParam.qry_beginDate" type="date" :picker-options="beginDatePickerOptions" style="width: 220px" placeholder="请选择开始时间" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" label-width="70px" v-if="refName === 'feedbackHistory'">
          <el-date-picker v-model="queryParam.qry_endDate" :picker-options="endDatePickerOptions" type="date" style="width: 220px" placeholder="请选择结束时间" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
      </template>
    </tl-list-search>
    <div>
      <!--表格渲染-->
      <el-table class="tl-table" ref="mainTable" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" row-key="id" border fit :row-class-name="doChangeRowClassName" :cell-style="{padding:'0'}">
        <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
        <el-table-column prop="content" label="反馈问题" :show-overflow-tooltip="true" min-width="280px" sortable="custom" sort-by="content"></el-table-column>
        <el-table-column prop="dispsystem" label="所属系统" width="150px" sortable="custom" sort-by="system" align="center"></el-table-column>
        <el-table-column prop="submitdate" label="提交时间" width="180px" sortable="custom" sort-by="submitdate" align="center"></el-table-column>
        <el-table-column label="操作" width="100px" align="center" fixed="right" v-if="approvalState === 1">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-position" @click="openApprovalDialog(scope.row.id)" :disabled="scope.row.state === 2 || scope.row.state === 3 || scope.row.state === 8">审批</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100px" align="center" fixed="right" v-else>
          <template slot-scope="scope">
            <el-button type="text" @click="openFeedbackDialog(scope.row.id)">{{ operateType }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-row class="pagebar" ref="pager">
        <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"></el-pagination>
      </el-row>
      <approval-edit ref="approvalForm" @refresh="doRefresh"></approval-edit>
      <feedback-dialog ref="feedbackDialog" @refresh="doRefresh"></feedback-dialog>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
import { queryDataCode } from '@tapui/api/dataCode'
import { getAction } from '@tapui/utils/http'
/** 子组件 */
import tlSearch from '@tapui/components/Tap/List/ListSearch'
import tlToolbar from '@tapui/components/Tap/List/ListToolBar'
import tlRefTreeModule from '@tapui/components/Tap/Ref/RefTree'
import approvalEdit from './approvalEdit'
import approvalOne from '../../cbo/cboApprovalNodeIns/approvalOne'
import feedbackDialog from '../mmsSuggestionFeedback/feedbackDialog'
import { validateNull } from 'tap-ui/src/utils/validate'
import { filterObjectNull } from 'tap-ui/src/utils/util'
import { MMS_SYSTEM_TYPE } from '@/const/const'
export default {
  name: 'myApprovalList',
  components: { approvalEdit, tlSearch, tlToolbar, tlRefTreeModule, approvalOne, feedbackDialog },
  mixins: [mixin_list],
  data() {
    return {
      description: '待审批列表页',
      url: {
        count: '/cbo/approvalNodeIns/querySouceTypeCount',
        queryMyApprovalList: '/mms/suggestionFeedback/queryMyApprovalList',
        queryCodeList: '/mms/suggestionFeedback/dataCodeList',
        list: '/mms/suggestionFeedback/list'
      },
      stateList: [],
      sourcetypeList: [],
      sorterInfo: {
        // 排序参数
        sidx: 'feedback.submitdate',
        sord: 'desc'
      },
      displayDeptName: '',
      isQueryOnCreated: false,
      approvalInfoList: '',
      tempQueryUrl: '',
      userRole: [],
      // 禁用结束日期之后的
      beginDatePickerOptions: {
        disabledDate: (date) => {
          // 结束日期清空，不禁用开始时间
          if (this.queryParam.qry_endDate === null) {
            return new Date(date).getTime() > new Date().getTime()
          } else {
            return new Date(date).getTime() > new Date(this.queryParam.qry_endDate).getTime() || new Date(date).getTime() > new Date().getTime()
          }
        }
      },
      // 禁用开始日期之前的
      endDatePickerOptions: {
        disabledDate: (date) => {
          return new Date(date).getTime() < new Date(this.queryParam.qry_beginDate).getTime() || new Date(date).getTime() > new Date().getTime()
        }
      },
      myApprovalLoadTimes: 1
    }
  },
  computed: {
    operateType() {
      if (this.refName === 'feedbackList') {
        return '编辑'
      } else {
        return '查看'
      }
    },
    bpaSystemFlag() {
      return this.$store.getters.microGlobalParam.systemFlag === 'bpa'
    }
  },
  props: {
    approvalState: {
      type: Number,
      default: 0
    },
    feedbackState: {
      type: Number,
      default: 0
    },
    refName: {
      type: String,
      default: ''
    }
  },
  methods: {
    afterLoadData() {
      if (this.refName === 'myApproval') {
        // 更新待办数量
        this.$emit("approvalCount", this.pageInfo.totalCount)
      }
    },
    openApprovalDialog(id) {
      this.$refs.approvalForm.nodeInsId = id
      this.$refs.approvalForm.visible = true
    },
    openFeedbackDialog(id) {
      if (this.refName === 'feedbackList') {
        this.$refs.feedbackDialog.isManager = true
      }
      this.$refs.feedbackDialog.id = id
      this.$refs.feedbackDialog.visible = true
      this.$refs.feedbackDialog.parentName = this.refName
    },
    doRowDbClick(row, column, event) {
      if (row.approvalState === 4) {
        return
      }
      if (this.approvalState === 1) {
        this.openApprovalDialog(row.id)
      } else {
        this.openFeedbackDialog(row.id)
      }
    },
    async loadData(paramMap) {
      // 得到加载分页
      if (validateNull(paramMap) === false && Object.keys(paramMap).indexOf('currentPage') > -1) {
        this.pageInfo.currentPage = paramMap['currentPage']
      }
      if (!this.beforeLoadData()) return
      // 查询条件
      let params = this.getQueryParams()
      // 数据查询
      this.loading = true
      if (this.refName === 'feedbackList' || this.refName === 'feedbackHistory') {
        this.tempQueryUrl = this.url.list
      } else {
        this.tempQueryUrl = this.url.queryMyApprovalList
      }
      this.$set(params, "refName", this.refName)
      if (this.refName === 'feedbackList' && this.userRole.length > 0) {
        let tempSystem = []
        this.userRole.forEach(item => {
          tempSystem.push(item.system)
        })
        this.$set(params, "rolecode", this.userRole[0].role)
        this.$set(params, "systemcodes", tempSystem.join(","))
      }
      await getAction(this.tempQueryUrl, params)
        .then((res) => {
          if (res.success) {
            this.datas = res.result.dataList
            this.pageInfo.totalCount = res.result.totalCount
            // 初始化自定义列
            if (typeof this.$refs.toolbar !== 'undefined') {
              this.$refs.toolbar.doInitTableColumn()
            }
          } else {
            return new Promise((resolve, reject) => {
              reject(res.message)
            })
          }
          this.afterLoadData()
          this.loading = false
        })
        .catch((err) => {
          this.loading = false
        })
    },
    getQueryParams() {
      let param = {}
      if (this.superQueryParams) {
        param['superQueryParams'] = encodeURI(this.superQueryParams)
      }
      // 结束时间
      if (this.queryParam.qry_endDate) {
        let date = new Date(this.queryParam.qry_endDate)
        let timeDate = ""
        let Y = date.getFullYear() + '-'
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
        let h = 23 + ':'
        let m = 59 + ':'
        let s = 59
        timeDate = Y + M + D + h + m + s
        this.queryParam.qry_endDate = timeDate
      }
      if (this.bpaSystemFlag) {
        // 当前页面由流程文档跳入传入该参数，否则查询全部
        this.filtersInfo.systemFlag = MMS_SYSTEM_TYPE.cdmc
      }
      Object.assign(param, this.queryParam, this.sorterInfo, this.filtersInfo)
      param.currentPage = this.pageInfo.currentPage
      param.pageSize = this.pageInfo.pageSize
      param.qry_code = this.$store.getters.user.code
      if (this.feedbackState === 9) {
        param.feedbackState = this.feedbackState
      } else {
        param.qry_sourcetype = 'MmsSuggestionFeedback'
        param.qry_approvalState = this.approvalState
      }
      return filterObjectNull(param)
    }
  },
  created() {
    queryDataCode({ itemCodes: 'TapApprovalNodeState' }).then((res) => {
      this.stateList = res.result.TapApprovalNodeState
    })
    getAction(this.url.count).then((res) => {
      this.sourcetypeList = res.result
    })
    if (this.$route.query.sourcetype) {
      this.$set(this.queryParam, 'qry_sourcetype', this.$route.query.sourcetype)
    }
    // 我的待办默认按照提交时间正序排列
    if (this.refName === 'myApproval') {
      this.$set(this.sorterInfo, 'sord', 'asc')
    }
    this.loadData({})
  }
}
</script>
<style scoped lang="scss">
.count {
  float: right;
  .mark {
    padding-top: 5px;
  }
}
.red-bg {
  background: red;
}
</style>
